
package com;

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       
        int[] ints = new int[10];
        for (int anInt : ints) {
            
        }
        Arrays.setAll(ints, i -> (1-i));
        int[] ints2 = Arrays.copyOf(ints, ints.length);
        List<int[]> ints1 = Arrays.asList( );

//        ints1.sort((o1,o2)->o1-o2);
        System.out.println(ints);
        //HashMap中Lambda表达式的使用
//        HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>(15);
//        integerIntegerHashMap.put(1,2);
//        integerIntegerHashMap.put(2,2);
//        integerIntegerHashMap.put(7,2);
//        integerIntegerHashMap.forEach((k,v)-> System.out.println(k + "=" + v));



    }

    public static void main1(String[] args) {
        int[] arr = {1, 2, 5, 6, 7, 5, 4, 2, 19, 3, 5};
//        HashSet<Integer> set1 =new HashSet<Integer>();
//        HashSet<Integer> set2 =new HashSet<Integer>();
//        HashMap<Integer,Integer> map= new HashMap<Integer,Integer>();
//        for (int i = 0; i < arr.length; i++) {
//            if(map.get(arr[i])==null){
//                map.put(arr[i],1);
//            }else{
//                int tmp=map.get(arr[i]);
//                map.put(arr[i],++tmp);
//            }
//        }
//        for(Map.Entry<Integer,Integer> entry: map.entrySet()){
//            System.out.println(entry.getKey()+"="+entry.getValue());
//        }
//        numJewelsInStones(" ", " ");
        String[] arr1 = {"i", "love", "leetcode", "i", "love", "coding"};

        topKFrequent(arr1, 1);

    }

    public static int numJewelsInStones(String jewels, String stones) {
        jewels = "aA";
        stones = "aAAbbbb";
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i));
        }
        int count = 0;
        for (int i = 0; i < stones.length(); i++) {
            if (set.contains(stones.charAt(i))) ;
            {
                count++;
            }
        }
        return count;

    }

    public static List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        //利用哈希桶统计单词出现的频率
        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }
        //找出现最多的k个，建立小根堆。
        PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<Map.Entry<String, Integer>>(k, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                int ret = o1.getValue().compareTo(o2.getValue()) == 0 ? 0 : o1.getValue().compareTo(o2.getValue());
                int ret1 = ret == 0 ? o1.getKey().compareTo(o2.getKey()) : ret;
                return ret1;
            }

            ;
        });
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
            if (pq.size() < k) {
                pq.offer(stringIntegerEntry);
            } else {
                Map.Entry<String, Integer> peek = pq.peek();
                if (peek.getValue() < stringIntegerEntry.getValue()) {
                    pq.poll();
                    pq.offer(stringIntegerEntry);
                } else {
                    if (stringIntegerEntry.getValue().compareTo(peek.getValue()) == 0) {
                        if (peek.getKey().compareTo(stringIntegerEntry.getKey()) > 0) {
                            pq.poll();
                            pq.offer(stringIntegerEntry);
                        }
                    }
                }
            }
        }
        List<String> ret = new LinkedList<>();
        for (int i = 0; i < k; i++) {
            ret.add(pq.poll().getKey());
        }
        Collections.reverse(ret);
        System.out.println(ret);
        return ret;


    }
}